<template>
  <div class="inv-container">
    <div>
      <el-form class="demo-form-inline" :inline="true">
        <el-row>
          <el-form-item>
            <el-button :icon="Search" plain type="primary" @click="openQuery">
              查询
            </el-button>
            <el-button :icon="Plus" plain type="primary" @click="toAdd">
              添加
            </el-button>
            <el-button :icon="Delete" plain type="warning" @click="toDelete">
              删除
            </el-button>
            <el-button
              :icon="SuccessFilled"
              plain
              type="primary"
              @click="toAdd"
            >
              保存
            </el-button>
            <el-button :icon="Download" plain type="primary" @click="syncInv">
              同步
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div style="height: 20px"></div>
    <el-row>
      <el-col :span="4">
        <div class="custom-tree-node-container">
          <el-tree
            :data="state.dataSource"
            :highlight-current="true"
            node-key="pkInvcl"
            :props="{ children: 'downs', label: 'invclName' }"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20">
        <el-table
          :data="state.invList"
          highlight-current-row
          size="small"
          stripe
          @row-dblclick="invRowDbClick"
        >
          <el-table-column
            align="left"
            label="存货编码"
            prop="invcode"
            width="220"
          />
          <el-table-column
            align="left"
            label="存货名称"
            prop="invname"
            width="220"
          />
          <el-table-column label="规格" prop="invspec" width="180" />
          <el-table-column label="型号" prop="invtype" width="180" />
          <el-table-column label="大类" prop="invclName" width="130" />
          <el-table-column label="单位" prop="measName" width="130" />
          <el-table-column label="默认仓库" prop="whName" width="130" />
          <el-table-column label="是否自制" width="100">
            <template #default="scope">
              <el-icon v-if="scope.row.isSelf" color="#30DF80" size="20">
                <Select />
              </el-icon>
              <el-icon v-else color="#DF3730" size="20">
                <SemiSelect />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column label="生产耗用" width="100">
            <template #default="scope">
              <el-icon v-if="scope.row.isComsume" color="#30DF80" size="20">
                <Select />
              </el-icon>
              <el-icon v-else color="#DF3730" size="20">
                <SemiSelect />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column label="是否首检" width="100">
            <template #default="scope">
              <el-icon v-if="scope.row.isShoujian" color="#30DF80" size="20">
                <Select />
              </el-icon>
              <el-icon v-else color="#DF3730" size="20">
                <SemiSelect />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column label="是否销售" width="100">
            <template #default="scope">
              <el-icon v-if="scope.row.isSale" color="#30DF80" size="20">
                <Select />
              </el-icon>
              <el-icon v-else color="#DF3730" size="20">
                <SemiSelect />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column label="是否巡检" width="100">
            <template #default="scope">
              <el-icon v-if="scope.row.patrolInspect" color="#30DF80" size="20">
                <Select />
              </el-icon>
              <el-icon v-else color="#DF3730" size="20">
                <SemiSelect />
              </el-icon>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="paginationParams.pageNo"
          :layout="layout"
          :page-size="paginationParams.pageSize"
          :total="paginationParams.total"
          @current-change="handlePaginationCurrentChange"
          @size-change="handlePaginationSizeChange"
        />
      </el-col>
    </el-row>
    <InvInfo ref="iio" @save-callback="saveCallback" />
    <QueryDialogVue ref="queryDialogRef" @query-callback="queryCallback" />
    <confirmSyncDialog
      ref="confirmSyncDialogRef"
      @success-callback="syncCallback"
    />
  </div>
</template>

<script setup>
  import { getInventoryPageApi } from '@/api/base/inventory/index'
  import { getAllInvclApi } from '@/api/base/invcl/index'
  import {
    Plus,
    Delete,
    SuccessFilled,
    Search,
    Select,
    SemiSelect,
    Download,
  } from '@element-plus/icons-vue'
  import InvInfo from './modules/InvInfo.vue'
  import QueryDialogVue from './modules/queryDialog.vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import confirmSyncDialog from './modules/confirmSyncDialog.vue'
  const $baseMessage = inject('$baseMessage')

  const queryDialogRef = ref()
  const confirmSyncDialogRef = ref()

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const state = reactive({
    dataSource: [],
    invList: [],
    invclInfo: {},
    invcode: '',
    invname: '',
    invspec: '',
    invtype: '',
  })

  const openQuery = () => {
    queryDialogRef.value.openDialog()
  }
  const queryCallback = (query) => {
    state.invcode = query.invcode
    state.invname = query.invname
    state.invspec = query.invspec
    state.invtype = query.invtype
    getInvData()
  }

  const getInvData = async () => {
    const params = {
      pageSize: paginationParams.pageSize,
      pageNum: paginationParams.pageNo,
      pkCorp: pkCorp,
      pkInvcl: state.invclInfo.pkInvcl,
      invcode: state.invcode,
      invname: state.invname,
      invspec: state.invspec,
      invtype: state.invtype,
    }
    const { code, data } = await getInventoryPageApi(params)
    if (code === 2000) {
      state.invList = data.list
      paginationParams.total = data.total
    }
  }

  const paginationParams = reactive({
    pageNo: 1,
    pageSize: 20,
    total: 0,
  })
  //#region 分类tree相关的方法
  const handleNodeClick = async (treeData) => {
    state.invclInfo = treeData
    getInvData()
  }
  //#endregion

  const invRowDbClick = (data) => {
    iio.value.toOpenInvInfoDialog(data.pkInv, '', '')
  }
  //#endregion

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNo = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNo = 1
  }
  //#endregion pagintion 结束

  //#region 调用后端接口获取 存货分类和存货档案数据
  const getInvClData = async () => {
    const { code, data } = await getAllInvclApi({ pkCorp: pkCorp })
    if (code === 2000) {
      state.dataSource = data
    }
  }
  //#endregion

  const toAdd = () => {
    if (state.invclInfo.downs.length === 0) {
      iio.value.toOpenInvInfoDialog(
        '',
        state.invclInfo.pkInvcl,
        state.invclInfo.name
      )
    } else {
      $baseMessage('请先选择相应的存货末级分类', 'warning')
    }
  }

  const saveCallback = () => getInvData()

  const syncInv = () => {
    if (!state.invclInfo.endFlag) {
      $baseMessage('请选择末级存货分类同步', 'warning')
      return
    }
    confirmSyncDialogRef.value.openDialog(state.invclInfo)
  }

  const syncCallback = () => getInvData()

  watch(paginationParams, () => {
    getInvData()
  })

  onMounted(() => {
    getInvClData(), getInvData()
  })

  const iio = ref()
</script>

<style scope>
  .is-penultimate > .el-tree-node__content {
    color: #626aef;
  }

  .el-tree-node.is-expanded.is-penultimate > .el-tree-node__children {
    display: flex;
    flex-direction: row;
  }
  .is-penultimate > .el-tree-node__children > div {
    width: 25%;
  }
  .inv-container {
    padding: 6px 6px 6px 6px;
  }
</style>
